SET(PROJECT_NAME DenseMapping)
CMAKE_MINIMUM_REQUIRED(VERSION 2.8.3)
PROJECT(${PROJECT_NAME})
SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/CMakeModules/")
SET(CMAKE_BUILD_TYPE Debug) # Release, RelWithDebInfo

set(CUDA_TOOLKIT_ROOT_DIR "/usr/local/cuda-7.5")
find_package(CUDA REQUIRED)
SET(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS};-O3)
#SET(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS};-O3 -use_fast_math)

# Specify compute capability
#list(APPEND CUDA_NVCC_FLAGS -arch=sm_30)
#list(APPEND CUDA_NVCC_FLAGS -gencode arch=compute_30,code=sm_30)

SET(CUDA_PROPAGATE_HOST_FLAGS OFF)

SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3")
IF (CMAKE_COMPILER_IS_GNUCC)
    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
ELSE ()
    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
ENDIF ()


option(BUILD_TESTS "Build test programs" OFF)
if (BUILD_TESTS)
    ADD_DEFINITIONS(-DRMD_BUILD_TESTS)
endif (BUILD_TESTS)

# NCC patch size (must be odd integer, typical values: 3, 5, 7)
ADD_DEFINITIONS(-DRMD_CORR_PATCH_SIDE=5)
# Maximum extent of epipolar line search in pixels
ADD_DEFINITIONS(-DRMD_MAX_EXTENT_EPIPOLAR_SEARCH=100)

find_package(OpenCV REQUIRED)
find_package(Eigen REQUIRED)
find_package(Boost REQUIRED COMPONENTS system filesystem)
find_package(PCL REQUIRED)

set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)


include_directories(
        include
        ${OpenCV_INCLUDE_DIRS}
        ${Eigen_INCLUDE_DIRS}
        ${Boost_INCLUDE_DIRS}
        ${PCL_INCLUDE_DIRS}
)
add_definitions( ${PCL_DEFINITIONS} )
link_directories( ${PCL_LIBRARY_DIRS} )

# Set link libraries
list(APPEND LINK_LIBS
        ${OpenCV_LIBS}
        ${Boost_LIBRARIES}
        ${PCL_LIBRARIES}
        )


# Set CUDA sourcefiles
list(APPEND CU_SOURCE_FILES
        src/check_cuda_device.cu
        src/seed_matrix.cu
        src/depthmap_denoiser.cu
        src/reduction.cu
        )

# Set sourcefiles
list(APPEND SOURCE_FILES
        src/depthmap.cpp
        )

CUDA_ADD_LIBRARY(
        DenseMappingCuda
        ${CU_SOURCE_FILES}
)

ADD_LIBRARY(
        DenseMapping
        ${SOURCE_FILES}
)

target_link_libraries(
        DenseMapping
        ${LINK_LIBS}
)


## Test on dataset
add_executable(
        dataset_main
        test/dataset_main.cpp
        test/dataset.cpp
#        src/point_cloud.cpp
)

target_link_libraries(
        dataset_main
        DenseMapping
        DenseMappingCuda
)

add_subdirectory(libSGM)

# GTest
if (BUILD_TESOFF)
    enable_testing()
    #find_package(GTest REQUIRED)
    # Accepts the following variable as input:
    # GTEST_ROOT - (as a CMake or environment variable)
    #              The root directory of the gtest install prefix

    include_directories(${GTEST_ROOT}/include)
    list(APPEND GTEST_LINK_LIBS
            ${GTEST_BOTH_LIBRARIES}
            pthread
            )

    CUDA_ADD_LIBRARY(
            dense_map_test
            test/copy.cu
            test/sobel.cu
    )

    # Set sourcefiles for the tests
    list(APPEND TEST_SOURCE_FILES
            test/device_image_test.cpp
            test/seed_matrix_test.cpp
            test/main_test.cpp
            test/dataset.cpp
            test/epipolar_test.cpp
            test/reduction_test.cpp
            )

    add_executable(
            all_tests
            ${TEST_SOURCE_FILES}
    )

    target_link_libraries(
            all_tests
            DenseMapping
            DenseMappingCuda
            dense_map_test
            ${GTEST_LINK_LIBS}
    )

    # Allow run of 'make test'
    add_test(
            RMDCuTests
            all_tests --gtest_repeat=2
    )
endif (BUILD_TESOFF)
